//
//  MYGExpertTaskDetailViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/30.
//

import UIKit

class MYGExpertTaskDetailViewController: MYGBaseController {

    var id:Int = 0
    private var model: MYGAcademicExpertModel?
    private var contents:[MYGAcademicExpertSession_infoModel] = []
    private var datas:[MYGAcademicExpertTaskListModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        getData()
    }
    
    func configUI(){
        view.backgroundColor = .white
        view.addSubview(headerView)
        headerView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(300) + statusBarHeight)
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom).offset(15)
            make.bottom.equalToSuperview()
        }
        
    }
    
     private lazy var headerView: MYGExpertTaskDetailHeaderView = {
         let view = MYGExpertTaskDetailHeaderView()
         view.didSelectCell = { [weak self] index in
             self!.contents = self!.datas[index].session_info
             self!.tableView.reloadData()
         }
         return view
     }()
    override var hideNavigationController: Bool {
        true
    }
    
    private lazy var tableView: UITableView = {
        let tableV = UITableView(frame: .zero, style: .grouped)
        tableV.backgroundColor = .rgba(245, 245, 245, 1)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.showsVerticalScrollIndicator = false
        tableV.register(cellWithClass: MYGExpertTaskDetailCell.self)
        return tableV
    }()
    
}

extension MYGExpertTaskDetailViewController {
    
    private func getData(){
        var params :[String:Any] = [:]
        params["id"] = id
        
        MYGLoading.show()
        MMYGNetwork<MYGHomePageAPI,MYGAcademicExpertModel>.request(target: .expert_detail(param: params)) { [self] result in
            MYGLoading.hide()
            guard let item = result else {
                return
            }
            self.model = item
            headerView.getHeaderData(model: item)
            self.datas = item.task_list
            if (self.datas.count > 0){
                self.contents = self.datas[0].session_info
            }
            self.tableView.reloadData()
            //测试日期
//            dataFactory()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    private func dataFactory() {
        
        self.datas.forEach { model in
            
            model.session_info.forEach { obj in
                
                
            }
            
        }
        
        
        
        
        
    }
    
}

extension MYGExpertTaskDetailViewController:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return self.contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
     }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGExpertTaskDetailCell.self)
        cell.model = self.contents[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRect(x: 0, y: 10, width: Int(screenWidth), height: 30))
        v.backgroundColor = .white
        let line = UIView()
        line.backgroundColor = .rgba(73, 117, 236, 1)
        v.addSubview(line)
        
        let tipLab = UILabel()
        tipLab.text = "任务列表 (\(self.contents.count))"
        tipLab.textColor = .rgba(34, 34, 34, 1)
        tipLab.font = .medium(18)
        
        v.addSubview(tipLab)
        line.snp.makeConstraints { make in
            make.left.equalTo(17.5)
            make.height.equalTo(15)
            make.width.equalTo(3)
            make.top.equalToSuperview().offset(17.5)
        }
        
        tipLab.snp.makeConstraints { make in
            make.centerY.equalTo(line)
            make.left.equalTo(line.snp.right).offset(13)
            make.height.equalTo(25)
        }
       
        return v
    }
}
